﻿Imports System.Net
Imports System.IO
Imports SevenZip

Module Orange
    Public Sub RecupererWebTVOrange()
        Dim infoslicences As Dictionary(Of String, Object) = telecharger("http://chaines-tv.orange.fr/pfs-webapp/user/liveInfos.json", False)

        Dim licences As ArrayList = CType(CType(infoslicences.ElementAt(1).Value, Dictionary(Of String, Object)).ElementAt(3).Value, ArrayList)

        Dim infoschaines As Dictionary(Of String, Object) = telecharger("http://chaines-tv.orange.fr/pfs-webapp/live/servicePlan.json", True)

        Dim chaines As ArrayList = CType(infoschaines.ElementAt(1).Value, ArrayList)
        Dim listeChaines As New Dictionary(Of String, String)

        For Each chaine As Dictionary(Of String, Object) In chaines
            For Each licence As String In licences
                If chaine.ElementAt(0).Value.ToString = licence Then

                    Dim infosChaineMedium As Dictionary(Of String, Object) = telecharger("http://chaines-tv.orange.fr/pfs-webapp/user/live/channel/" & chaine.ElementAt(17).Value.ToString & "/url.json?resolution=MEDIUM&subtitle=false", False)
                    Dim infosChaineLow As Dictionary(Of String, Object) = telecharger("http://chaines-tv.orange.fr/pfs-webapp/user/live/channel/" & chaine.ElementAt(17).Value.ToString & "/url.json?resolution=LOW&subtitle=false", False)



                    If infosChaineLow.ElementAt(0).Key.ToString.Trim <> "error" Then
                        listeChaines.Add(chaine.ElementAt(15).Value.ToString & "(Low)", infosChaineLow.ElementAt(1).Value.ToString.Replace("http://", "mmsh://CDLYOS02.se."))
                    End If
                    If infosChaineMedium.ElementAt(0).Key.ToString.Trim <> "error" Then
                        listeChaines.Add(chaine.ElementAt(15).Value.ToString & "(Medium)", infosChaineMedium.ElementAt(1).Value.ToString.Replace("http://", "mmsh://CDLYOS02.se."))
                    End If
                End If
            Next
        Next

        If listeChaines.Count = 0 Then
            MessageBox.Show("Pas de chaînes trouvées." & Environment.NewLine & "Êtes vous bien abonné chez Orange France ?", "Pas de chaînes trouvées !", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            Dim IDPlayList As Int64 = InsertPlaylist("WebTV Orange")
            Dim ordre As Integer = 1
            For Each kvpChaine As KeyValuePair(Of String, String) In listeChaines
                InsertChannel(IDPlayList, kvpChaine.Value, kvpChaine.Key, "", ordre)
                ordre += 1
            Next
        End If

    End Sub

    Private Function telecharger(ByVal url As String, ByVal unzip As Boolean) As Dictionary(Of String, Object)
        Dim myWebClient As New WebClient()
        Dim apppath As String = Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\"c))
        Dim temppath As String = System.IO.Path.GetTempPath
        Static cpt As Integer = 0

        Dim nom As String = NomFichier(url)
        Dim extension As String = ExtensionFichier(url)

        Dim uri As New Uri(url)

        Dim fichierlocal As String = temppath & nom & cpt.ToString & "." & extension

        If File.Exists(fichierlocal) Then
            File.Delete(fichierlocal)
        End If
        myWebClient.DownloadFile(uri, fichierlocal)

        If unzip Then

            Dim fichierlocaldezip As String = temppath & nom
            If File.Exists(fichierlocaldezip) Then
                File.Delete(fichierlocaldezip)
            End If
            Dim dezip As New SevenZipExtractor(fichierlocal)
            If dezip.Check Then
                dezip.ExtractFiles(temppath, {0})
            End If
            Dim sr As New StreamReader(temppath & nom & cpt.ToString & ".tar")
            Dim lejson As String = sr.ReadToEnd

            Return CType(fastJSON.JSON.Instance.Parse(lejson), Dictionary(Of String, Object))

        Else
            Dim sr As StreamReader = File.OpenText(fichierlocal)
            Dim lejson As String = sr.ReadToEnd
            sr = Nothing
            cpt += 1
            Return CType(fastJSON.JSON.Instance.Parse(lejson), Dictionary(Of String, Object))

        End If

    End Function

    Private Function NomFichier(ByVal fichier As String) As String
        Dim debut As Integer = fichier.LastIndexOf("/"c) + 1
        Dim taille As Integer = fichier.Length - debut - (fichier.Length - fichier.LastIndexOf("."))
        Return fichier.Substring(debut, taille)
    End Function

    Private Function ExtensionFichier(ByVal fichier As String) As String
        Dim debut As Integer = fichier.LastIndexOf("."c) + 1
        Dim taille As Integer
        If fichier.IndexOf("?"c) = -1 Then
            taille = fichier.Length - debut
        Else
            taille = fichier.Length - debut - (fichier.Length - fichier.IndexOf("?"c))
        End If
        Return fichier.Substring(debut, taille)
    End Function


End Module
